﻿using Apps.BLL.Report;
using Apps.Common;
using Apps.Models;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace ReportViewerForMvc
{
    /// <summary>
    /// The Web Form used for rendering a ReportViewer control.
    /// </summary>
    public partial class BuckleTonneWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ReportViewer1.Drillthrough += new DrillthroughEventHandler(ReportViewer1_Drillthrough);
            BuildReportViewer();
        }

        private void BuildReportViewer()
        {
            if (!IsPostBack)
            {
                ReportViewerForMvc.ReportViewer.ID = ReportViewer1.ID;

                ReportViewer1.SetProperties(ReportViewerForMvc.ReportViewer);
            }
        }

        protected void ReportViewer1_Drillthrough(object sender, DrillthroughEventArgs e)
        {
            LocalReport localReport = (LocalReport)e.Report;
            IList<ReportParameter> list = localReport.OriginalParametersToDrillthrough;
            if (localReport.ReportPath.IndexOf("BuckleTonneDetail_Report") >= 0)
            {
              
                var whereLambda = PredicateBuilder.True<V_RC_UPLOADING>();
                whereLambda = whereLambda.And<V_RC_UPLOADING>(m => m.STATUS == 2);

                string RpTIME = "", TRANSPORT_ID = "", SUPPLIER_ID = "";
                RpTIME = list.Where(m => m.Name == "RpTime").FirstOrDefault().Values[0].ToString();

                TRANSPORT_ID = list.Where(m => m.Name == "TRANSPORT_ID").FirstOrDefault().Values[0].ToString();
                SUPPLIER_ID = list.Where(m => m.Name == "SUPPLIER_ID").FirstOrDefault().Values[0].ToString();
                List<V_RC_UPLOADING> Data = new List<V_RC_UPLOADING>();

                localReport.EnableHyperlinks = true;
                if (!string.IsNullOrWhiteSpace(RpTIME))
                {
                    DateTime FDATETime = Convert.ToDateTime(RpTIME).Date;
                    whereLambda = whereLambda.And<V_RC_UPLOADING>(m => m.DEDUCTION_TIME >= FDATETime);
                }
                whereLambda = whereLambda.And<V_RC_UPLOADING>(m => m.TRANSPORT_ID == TRANSPORT_ID);
                whereLambda = whereLambda.And<V_RC_UPLOADING>(m => m.SUPPLIER_ID == SUPPLIER_ID);

                Data = ReportBLL<V_RC_UPLOADING>.GetReportDataList(whereLambda);

                localReport.DataSources.Add(new ReportDataSource { Value = Data, Name = "DataSet1" });
            }
            else
            {
                localReport.EnableExternalImages = true;

               
                string UPLOADING_ID = list.Where(m => m.Name == "UPLOADING_ID").FirstOrDefault().Values[0].ToString();
                var Data = ReportBLL<RC_UPLOADING_IMAGE>.GetReportDataList(m => m.UPLOADING_ID == UPLOADING_ID);
                if (Data.Count > 0)
                {
                    Data[0].METERIEL_IMAGE2 = Convert.ToBase64String(Data[0].METERIEL_IMAGE);
                    Data[0].CAR_IMAGE2 = Convert.ToBase64String(Data[0].CAR_IMAGE);
                }
                localReport.DataSources.Add(new ReportDataSource { Value = Data, Name = "DataSet1" });
            }

        }

        
    }
}